﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-4-18 9:55:35
 ******************************************************************************/
using System;
using System.Diagnostics;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.Utilities;

    public partial class PiUserManager:DbCommonManager,IDbCommonManager
    {
        /// <summary>
        /// 批量设置密码
        /// </summary>
        /// <param name="userIds">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <returns>影响行数</returns>
        public int SetPassword(string[] userIds, string password)
        {
            //int returnValue = 0;

            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                password = SecretHelper.AESEncrypt(password);
            }

            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
            sqlBuilder.BeginUpdate(this.CurrentTableName);
            sqlBuilder.SetValue(PiUserTable.FieldUserPassword, password);
            sqlBuilder.SetDBNow(PiUserTable.FieldChangePasswordDate);

            sqlBuilder.SetWhere(PiUserTable.FieldId, userIds);
            return sqlBuilder.EndUpdate();

            //string[] targetFields = new string[] { PiUserTable.FieldUserPassword, PiUserTable.FieldChangePasswordDate };
            //object[] targetValues = new object[] { password, System.DateTime.Now};
            // 设置密码字段
            // returnValue += DbCommonLibary.SetProperty(DBProvider, PiUserTable.TableName, PiUserTable.FieldId, userIds, targetFields, targetValues);

            // return returnValue;
        }

        /// <summary>
        /// 用户密码加密处理功能
        /// 
        /// 用户的密码到底如何加密，数据库中如何存储用户的密码？
        /// 若是明文方式存储，在管理上会有很多漏洞，虽然调试时不方便，当时加密的密码相对是安全的，
        /// 而且最好是密码是不可逆的，这样安全性更高一些，各种不同的系统，这里适当的处理一下就饿可以了。
        /// </summary>
        /// <param name="password">用户密码</param>
        /// <returns>处理后的密码</returns>
        public virtual string EncryptUserPassword(string password)
        {
            // 这里也可以选择不进行处理，不加密
            // return password;
            return SecretHelper.AESEncrypt(password);
        }

        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="userId">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <returns>影响行数</returns>
        public virtual int SetPassword(string userId, string password)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            int returnValue = 0;

            // 密码强度检查
            /*
            if (SystemInfo.EnableCheckPasswordStrength)
            {
                if (password.Length == 0)
                {
                    this.ReturnStatusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return returnValue;
                }
            }
            */

            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                password = this.EncryptUserPassword(password);
            }
            // 设置密码字段
            string[] targetFields = new string[] { PiUserTable.FieldUserPassword, PiUserTable.FieldChangePasswordDate };
            Object[] targetValues = new object[] { password, null };
            // Object[] targetValues = new object[] { password, System.DateTime.Now };
            returnValue = this.SetProperty(userId, targetFields, targetValues);
            if (returnValue == 1)
            {
                this.ReturnStatusCode = StatusCode.SetPasswordOK.ToString();
            }
            else
            {
                // 数据可能被删除
                this.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
            }

            // 写入调试信息
            #if (DEBUG)
            int milliEnd = Environment.TickCount;
            Trace.WriteLine(DateTime.Now.ToString(SystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " " + " BaseUserManager.SetPassword(" + userId + ")");
            #endif

            return returnValue;
        }

        /// <summary>
        /// 批量设置密码
        /// </summary>
        /// <param name="userIds">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <returns>影响行数</returns>
        public virtual int BatchSetPassword(string[] userIds, string password)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            int returnValue = 0;
            // 密码强度检查
            /*
            if (SystemInfo.EnableCheckPasswordStrength)
            {
                if (password.Length == 0)
                {
                    statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return returnValue;
                }
            }
            */
            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                password = this.EncryptUserPassword(password);
            }

            string[] targetFields = new string[] { PiUserTable.FieldUserPassword, PiUserTable.FieldChangePasswordDate };
            Object[] targetValues = new object[] { password, null };
            // 设置密码字段
            returnValue += DbCommonLibary.SetProperty(DBProvider, PiUserTable.TableName, PiUserTable.FieldId, userIds, targetFields, targetValues);

            if (returnValue > 0)
            {
                this.ReturnStatusCode = StatusCode.SetPasswordOK.ToString();
            }
            else
            {
                // 数据可能被删除
                this.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
            }

            // 写入调试信息
            #if (DEBUG)
            int milliEnd = Environment.TickCount;
            Trace.WriteLine(DateTime.Now.ToString(SystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " " + " BaseUserManager.SetPassword()");
            #endif

            return returnValue;
        }

        /*
        /// <summary>
        /// 批量设置通讯密码
        /// </summary>
        /// <param name="userIds">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <returns>影响行数</returns>
        public virtual int BatchSetCommunicationPassword(string[] userIds, string password, out string statusCode)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            int returnValue = 0;
            // 密码强度检查
            if (SystemInfo.EnableCheckPasswordStrength)
            {
                if (password.Length == 0)
                {
                    statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return returnValue;
                }
            }
            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                password = this.EncryptUserPassword(password);
            }
            for (int i = 0; i < userIds.Length; i++)
            {
                // 设置密码字段
                returnValue = DbCommonLibary.SetProperty(DBProvider, PiUserTable.TableName, PiUserTable.FieldId, userIds, PiUserTable.FieldCommunicationPassword, password);
            }
            if (returnValue > 0)
            {
                statusCode = StatusCode.SetPasswordOK.ToString();
            }
            else
            {
                // 数据可能被删除
                statusCode = StatusCode.ErrorDeleted.ToString();
            }

            // 写入调试信息
            #if (DEBUG)
            int milliEnd = Environment.TickCount;
            Trace.WriteLine(DateTime.Now.ToString(SystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " " + " BaseUserManager.SetPassword()");
            #endif

            return returnValue;
        }
         */

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <returns>影响行数</returns>
        public virtual int ChangePassword(string oldPassword, string newPassword, out string statusCode)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            int returnValue = 0;
            // 密码强度检查
            if (SystemInfo.EnableCheckPasswordStrength)
            {
                if (String.IsNullOrEmpty(newPassword))
                {
                    statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return returnValue;
                }
            }
            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                oldPassword = this.EncryptUserPassword(oldPassword);
                newPassword = this.EncryptUserPassword(newPassword);
            }
            // 判断输入原始密码是否正确
            PiUserEntity userEntity = new PiUserEntity();
            userEntity.GetFrom(this.GetDTById(UserInfo.Id));
            if (userEntity.UserPassword == null)
            {
                userEntity.UserPassword = string.Empty;
            }
            // 密码错误
            if (!userEntity.UserPassword.Equals(oldPassword))
            {
                statusCode = StatusCode.OldPasswordError.ToString();
                return returnValue;
            }
            // 更改密码，同时修改密码的修改日期
            string[] targetFields = new string[] { PiUserTable.FieldUserPassword, PiUserTable.FieldChangePasswordDate };
            Object[] targetValues = new object[] { newPassword, System.DateTime.Now };
            returnValue = this.SetProperty(UserInfo.Id, targetFields, targetValues);
            if (returnValue == 1)
            {
                statusCode = StatusCode.ChangePasswordOK.ToString();
            }
            else
            {
                // 数据可能被删除
                statusCode = StatusCode.ErrorDeleted.ToString();
            }

            // 写入调试信息
            #if (DEBUG)
            int milliEnd = Environment.TickCount;
            Trace.WriteLine(DateTime.Now.ToString(SystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " " + " BaseUserManager.ChangePassword(" + userEntity.Id + ")");
            #endif

            return returnValue;
        }
    }
}
